$(function () {
  $(window).on('scroll', handleScroll);
  $(document).on('click', '.dep-detail-date-btn', handleDateClick);

  function handleDateClick(e) {
    $('html').animate({ scrollTop: $('.dep-detail-date').offset().top + "px" }, 200);
    $('.dep-detail-date-btn').removeClass('dep-detail-date-btn-active');
    $(this).addClass('dep-detail-date-btn-active');
    var index = $('.dep-detail-date-btn').index(this);
    $('.dep-detail-pb-item.float-left.active').removeClass('active');
    $('.dep-detail-pb-content>div').each(function () {
      $(this).children('.dep-detail-pb-item.float-left').eq(index).addClass('active');
    });
  }

  function handleScroll(e) {
    var div = document.getElementsByClassName('dep-detail-date')[0];
    var b = div.getBoundingClientRect().top;
    if (b < 0 && (!$(div).hasClass('sticky-top'))) {
      var _left = div.getBoundingClientRect().left;
      var _width = $(div).width();
      $(div).addClass('sticky-top').css('left', _left + 'px').width(_width).data('position', $(window).scrollTop());
    } else if ($(div).data('position') && ($(window).scrollTop() < $(div).data('position')) && $(div).hasClass('sticky-top')) {
      $(div).removeClass('sticky-top').removeData('position');
    }
  }

  var departmentInfo = new Vue({
    el:"#departmentInfo",
    data:{
      departmentPageInfo:{},
      hospitalId:"",
      tdepartmentsId:"",
      hospitalUrl:"",
      doctorScheduleDtos:[],
      pageDoctorDtos:[],
      sreachDate:"",//搜索框数据,
      token:localStorage.getItem('token')
    },
    methods:{
      goDoctor:function(data){
        location.href= "doctor.html?doctorId="+data;
      },
      getDepartmentPageInfo:function () {
        var that = this;
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
          var pair = vars[i].split("=");
          if(pair[0] == "hospitalId"){
            that.hospitalId = pair[1];
          }
          if(pair[0] == "tdepartmentsId"){
            that.tdepartmentsId = pair[1];
          }
        }
        axios.get("../api/tdepartments/getPageTdepartmentsInfo?hospitalId="+that.hospitalId+"&tdepartmentsId="+that.tdepartmentsId).then(function(response){
          //alert(response.data.code)
          if (response.data.code!="0"){
            alert("获取数据失败");
            return;
          }
          console.log(response.data.data)
          that.departmentPageInfo = response.data.data;
          that.hospitalUrl = "hospital.html?hospitalId="+that.hospitalId;
          that.doctorScheduleDtos = response.data.data.pageDoctorDtos[0].doctorScheduleDtos;
          that.pageDoctorDtos = response.data.data.pageDoctorDtos;
          console.log(that.doctorScheduleDtos)
        }).catch(function(err){
          console.log(err);
        });
      },
      amPayment:function (doctorId,date,dayTime) {
        location.href = "payment.html?date="+date+"&doctorId="+doctorId+"&dayTime="+dayTime;
      },
      pmPayment:function (doctorId,date,dayTime) {
        location.href = "payment.html?date="+date+"&doctorId="+doctorId+"&dayTime="+dayTime;
      },
      toTdepartment:function () {
        var that = this;
        if (that.sreachDate == null || that.sreachDate == "" || that.sreachDate == undefined){
          alert("搜索数据不能为空")
          return false;
        }
        axios.get("../api/tdepartments/getTdepartmentsByCondition?hospitalId="+that.hospitalId+"&tdepartmentsName="+that.sreachDate).then(function(response){
          //alert(response.data.code)
          if (response.data.code!="0"){
            alert("没有相关科室")
            return;
          }
          console.log(response.data.data)
          location.href="department.html?hospitalId="+that.hospitalId+"&tdepartmentsId="+response.data.data;
        }).catch(function(err){
          console.log(err);
        });
      },
      toDoctor02:function () {
        var that = this;
        if (that.sreachDate == null || that.sreachDate == "" || that.sreachDate == undefined){
          alert("搜索数据不能为空")
          return false;
        }

        axios.get("../api/doctor/getDoctorByCondition?hospitalId="+that.hospitalId+"&doctorName="+that.sreachDate).then(function(response){
          if (response.data.code!="0"){
            alert("没有相关医生")
            return;
          }
          console.log(response.data.data)
          location.href="doctor.html?doctorId="+response.data.data;
        }).catch(function(err){
          console.log(err);
        });
      },
    },
    created(){
      this.getDepartmentPageInfo();
    }
  })

  var getTokenVue = new Vue({
    el:"#getToken",
    data:{
      token:localStorage.getItem("token")
    }
    ,methods:{

    }
  })

});